package com.wootion.cims.module.systemsetting.role.controller;

import com.alibaba.fastjson.JSONObject;
import com.wootion.cims.module.systemsetting.dictionary.model.LogAnotion;
import com.wootion.cims.module.systemsetting.role.model.SysRole;
import com.wootion.cims.module.systemsetting.role.service.SysRoleService;
import com.wootion.cims.response.ResponseResult;
import com.wootion.cims.response.ResultCode;
import io.netty.util.internal.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;


@RestController
@RequestMapping("/sysrole")
@Api(value = "角色管理")
@RequiresAuthentication
public class SysRoleController {

    @Resource
    SysRoleService sysRoleService;

    @PostMapping("/save")
    @LogAnotion(type = "新增",value = "0",incident = "角色信息",prefixContent = "角色名称")
    @ApiOperation(value = "角色管理-角色新增", httpMethod = "POST", notes = "{\"password\":\"123456\",\"role\":\"123456\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"123\",\"465\"],\"username\":\"123456\"}")
    public ResponseResult save(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("name")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return sysRoleService.insert(jsonObject);
    }

    @PostMapping("/update")
    @ApiOperation(value = "角色管理-角色修改", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\",\"password\":\"123456\",\"role\":\"741\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"741\",\"852\"],\"username\":\"741852\"}")
    public ResponseResult update(HttpServletRequest request, @RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("name")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return sysRoleService.update(request,jsonObject);
    }

    @PostMapping("/delete")
    @LogAnotion(type = "删除",value = "0",incident = "角色信息",prefixContent = "角色名称")
    @ApiOperation(value = "角色管理-角色删除", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult delete(@RequestBody JSONObject jsonObject) {

        if (!Optional.ofNullable(jsonObject.getString("id")).isPresent()){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }

        return sysRoleService.deleteById(jsonObject.getString("id"));
    }

    @PostMapping(path = "/list")
    @ApiOperation(value = "角色管理-列表查询", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult list(@RequestParam(defaultValue = "20") Integer limit,
                               @RequestParam(defaultValue = "1") Integer page) {
        return sysRoleService.selectPage(limit,page);
    }

    @PostMapping(path = "/alllist")
    @ApiOperation(value = "角色下拉框", httpMethod = "POST", notes = "{\"id\":\"255762da-3072-4b60-898b-5bc8142250c6\"}")
    public ResponseResult alllist() {
        return sysRoleService.selectAll();
    }

    @PostMapping(path = "/findbyid")
    @ApiOperation(value = "角色管理-列表查询", httpMethod = "POST", notes = "")
    public ResponseResult findbyid(@RequestParam String id) {
        if (!Optional.ofNullable(id).isPresent() || StringUtil.isNullOrEmpty(id)){
            return ResponseResult.builder().code(ResultCode.PARAM_NOT_COMPLETE.getCode()).data(ResultCode.PARAM_NOT_COMPLETE.getMessage()).build();
        }
        return sysRoleService.findById(id);
    }


    @PostMapping("/132")
    @ApiOperation(value = "此接口无实际作用,前端需要字段参照,联调完成之后可删", httpMethod = "POST", notes = "{\"password\":\"123456\",\"role\":\"123456\",\"tel\":\"123456\",\"remark\":\"123456\",\"communitys\":[\"123\",\"465\"],\"username\":\"123456\"}")
    public ResponseResult save(@RequestBody SysRole sysRole) {

        return null;
    }
}
